package com.dailyblue.java.mybatis.mybatis06;

import com.dailyblue.java.mybatis.mybatis06.bean.Dept;
import com.dailyblue.java.mybatis.mybatis06.bean.Emp;
import com.dailyblue.java.mybatis.mybatis06.mapper.DeptMapper;
import com.dailyblue.java.mybatis.mybatis06.mapper.EmpMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class App {
    private static SqlSession session, session2;
    private static EmpMapper mapper;
    private static EmpMapper mapper2;
    private static DeptMapper deptMapper;

    static {
        try {
            InputStream in = Resources.getResourceAsStream("config.xml");
            SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(in);
            session = ssf.openSession();
            session2 = ssf.openSession();
            mapper = session.getMapper(EmpMapper.class);
            mapper2 = session2.getMapper(EmpMapper.class);
            deptMapper = session.getMapper(DeptMapper.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void a() {
        List<Emp> list = mapper.find();
        list.forEach(System.out::println);
    }

    private static void b() {
        Emp emp = mapper.find1(6);
        System.out.println(emp);
    }

    private static void c() {
        Dept dept = deptMapper.findDeptByNo2(10);
        System.out.println(dept);
    }

    private static void d() {
        List<Emp> list = mapper.find2("程序员");
        list.forEach(System.out::println);
    }

    private static void e() {
        List<Emp> list = mapper.find2("程序员");
        session.close();
        List<Emp> list2 = mapper2.find2("程序员");
        list.forEach(System.out::println);
        list.forEach(System.out::println);
    }

    public static void main(String[] args) {
        e();
    }
}
